package com.ke.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ke.dao.ShopRepository;
import com.ke.pojo.BaseResp;
import com.ke.pojo.Cart;
import com.ke.pojo.Shop;
import com.ke.service.CartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author bochen
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ShopRepository shopRepository;
    @Override
    public BaseResp addCart(Integer pid, Integer uid) {
        Object o = redisTemplate.opsForHash().get("cart_" + uid.toString(), pid.toString());
        if (o!=null){
            String s1 = JSONObject.toJSONString(o);
            Cart cart1 = JSONObject.parseObject(s1, Cart.class);
            cart1.setNum(cart1.getNum()+1);
            redisTemplate.opsForHash().put("cart_"+uid,pid.toString(),cart1);
            return new BaseResp(0,"添加成功");
        }else {
            Optional<Shop> byId = shopRepository.findById(pid);
            if (byId.isPresent()){
                Shop shop=byId.get();
                Cart cart=new Cart();
                cart.setNum(1);
                BeanUtils.copyProperties(shop,cart);
                redisTemplate.opsForHash().put("cart_"+uid.toString(),pid.toString(),cart);
                return new BaseResp(0,"添加成功");
            }
            return null;
        }
    }

    @Override
    public BaseResp findAll(Integer uid) {
        Boolean aBoolean = redisTemplate.hasKey("cart_" + uid);
        if (aBoolean){
            List<Cart> list = redisTemplate.opsForHash().values("cart_" + uid);
                return new BaseResp(0,"ok",list);
            }else{return new BaseResp(2,"购物车是空的");
            }
    }
    @Override
    public BaseResp deleteOneCart(Integer pid, Integer uid) {
        Object o = redisTemplate.opsForHash().get("cart_" + uid, pid.toString());
        String s = JSONObject.toJSONString(o);
        Cart cart = JSONObject.parseObject(s, Cart.class);
        Integer i=cart.getNum();
        if (i<2){
            redisTemplate.opsForHash().delete("cart_"+uid,pid.toString());
            return new BaseResp(0,"ok");
        }else {
        cart.setNum(i-1);
        redisTemplate.opsForHash().put("cart_"+uid,pid.toString(),cart);
        return new BaseResp(0,"ok");}
    }

    @Override
    public BaseResp deleteCart(Integer pid, Integer uid) {
        redisTemplate.opsForHash().delete("cart_"+uid,pid.toString());
        return new BaseResp(0,"ok");
    }

    @Override
    public BaseResp deleteAll(Integer uid) {
        redisTemplate.delete("cart_"+uid);
        return new BaseResp(0,"ok");
    }

    @Override
    public BaseResp addCart10(Integer pid, Integer uid) {
        Object o = redisTemplate.opsForHash().get("cart_" + uid.toString(), pid.toString());
            String s1 = JSONObject.toJSONString(o);
            Cart cart1 = JSONObject.parseObject(s1, Cart.class);
            cart1.setNum(cart1.getNum()+10);
            redisTemplate.opsForHash().put("cart_"+uid,pid.toString(),cart1);
            return new BaseResp(0,"土豪,你已添加成功");
    }
}
